//
//  BRTopUpHistoryViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/10/14.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRTopUpHistoryViewController.h"
#import "BRTopUpHistoryCell.h"
#import "BRPayRecordDetailVC.h"

@interface BRTopUpHistoryViewController ()<QMUITableViewDelegate,QMUITableViewDataSource>
{
    NSInteger _offset;
}

@property (nonatomic,strong) NSArray *dataArr;

@end

@implementation BRTopUpHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.emptyLYView setImage:UIImageMake(@"error_order")];
    _offset  = 0;
    [self setUI];
    [self addRefreshActionForView:self.tableView];
    [self addLoadMoreActionForView:self.tableView];
    self.tableView.mj_footer.hidden = YES;
    [self requestHistory];
}

- (void)handleListRefreshAction {
    _offset = 0;
    [self requestHistory];
}

- (void)handleListLoadMoreAction {
    _offset += 20;
    [self requestHistory];
}

- (void)toDetail:(NSInteger)index {
    BRPayRecordDetailVC *vc = [BRPayRecordDetailVC new];
    vc.dto = self.dataArr[index];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - Request --
- (void)requestHistory {
    
    [self.tableView ly_startLoading];
    
    [[BRRequestManager  shared] GET:[BRAPI userTransfer:_offset] params:nil succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        
        if (self->_offset  == 0) {
            self.dataArr = @[];
            self.tableView.mj_footer.hidden = NO;
        }
        NSInteger oldCount = self.dataArr.count;
        NSArray *arr = [BRDealFlowModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
       
        self.dataArr = [self.dataArr arrayByAddingObjectsFromArray:arr];
        
        if (self.dataArr.count < oldCount + kPageSize || self.dataArr.count == 0 ) {
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView reloadData];
        self.tableView.ly_emptyView = self.emptyLYView;
        [self.tableView ly_endLoading];
        
        [self handleEndRefreshForView:self.tableView];
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        self.tableView.mj_footer.hidden = YES;
        [self handleEndRefreshForView:self.tableView];
        self.tableView.ly_emptyView = self.netErrorView;
        [self.tableView ly_endLoading];
    }];
}

#pragma mark - tableView Delegate --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return 5;
    return self.dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 34;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRTopUpHistoryCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"BRTopUpHistoryCell"];
    
    cell.dto = self.dataArr[indexPath.row];

    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self toDetail:indexPath.row];
}

#pragma mark - set UI --
- (void)setUI {
    
    [self br_createTop:Localized(@"wallet_transactionRecord")];
    
    
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.backgroundColor = UIColor.clearColor;

    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BRTopUpHistoryCell class]) bundle:nil] forCellReuseIdentifier:@"BRTopUpHistoryCell"];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
